package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import com.example.mybatisplusdemo.model.domain.Favorite;
import com.example.mybatisplusdemo.model.domain.Merchant;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.model.dto.YearUserActivityDTO;
import com.example.mybatisplusdemo.model.dto.YesrCountDTO;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IUserService;
import com.example.mybatisplusdemo.model.domain.User;
import springfox.documentation.spring.web.json.Json;

import java.io.Serializable;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-04
 * @version v1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger( UserController.class );

    @Autowired
    private IUserService userService;

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        User user = userService.getById(id);
        return JsonResponse.success(user);
    }
    //注册的方法
    @GetMapping("userRegister")
    public JsonResponse userRegister(User user){
        try {
            boolean result = userService.userRegister(user);
            return JsonResponse.success(result);
        } catch (Exception e) {
            return JsonResponse.failure("注册失败: " + e.getMessage());
        }
    }

    // 登录方法
    @GetMapping("userLogin")
    public JsonResponse userLogin(User user){
        User userResult = userService.loginByUsername(user);
        if (userResult != null) {
            return JsonResponse.success(userResult);
        }
        userResult = userService.loginByPhone(user);
        if (userResult != null) {
            return JsonResponse.success(userResult);
        }
        userResult = userService.loginByEmail(user);
        if (userResult != null) {
            return JsonResponse.success(userResult);
        }
        userResult = userService.loginByWechat(user);
        return JsonResponse.success(userResult);
    }


    //根据用户名登录
    @PostMapping("loginByUsername")
    public JsonResponse loginByUsername(@RequestBody User user) {
        User user_login = userService.loginByUsername(user);
        return JsonResponse.success(user_login);
    }

    //根据手机号登录
    @PostMapping("loginByPhone")
    public JsonResponse loginByPhone(@RequestBody User user) {
        User user_login = userService.loginByPhone(user);
        return JsonResponse.success(user_login);
    }
    //根据邮箱登录
    @PostMapping("loginByEmail")
    public JsonResponse loginByEmail(@RequestBody User user) {
        User user_login = userService.loginByEmail(user);
        return JsonResponse.success(user_login);
    }
    //根据微信登录
    @PostMapping("loginByWechat")
    public JsonResponse loginByWechat(@RequestBody User user) {
        User user_login = userService.loginByWechat(user);
        return JsonResponse.success(user_login);
    }

    // 注册方法 前端注册应该填写的信息包括：手机号/邮箱/微信号，密码，昵称，性别，头像，生日
    //根据手机号注册
    @PostMapping("registerByPhone")
    public JsonResponse registerByPhone(@RequestBody User user) {
        boolean isRegistered = userService.registerByPhone(user);
        //如果手机号已存在，返回失败
        return JsonResponse.success(isRegistered);
    }
    //根据邮箱注册
    @PostMapping("registerByEmail")
    public JsonResponse registerByEmail(@RequestBody User user) {
        boolean isRegistered = userService.registerByEmail(user);
        //如果邮箱已存在，返回失败
        return JsonResponse.success(isRegistered);
    }
    @PostMapping("registerByWechat")
    public JsonResponse registerByWechat(@RequestBody User user) {
        boolean isRegistered = userService.registerByWechat(user);
        //如果微信号已存在，返回失败
        return JsonResponse.success(isRegistered);
    }

    // 获取当前用户
    @GetMapping("getInfo")
    public JsonResponse getInfo(){
        User currentUserInfo = SessionUtils.getCurrentUserInfo();
        return JsonResponse.success(currentUserInfo); //把当前用户送到前端
    }

    // 更新用户信息
    @PostMapping("saveUpdatedUser")
    public JsonResponse saveUpdatedUser(@RequestBody User user) {
        boolean isUpdated = userService.updateById(user);
        return JsonResponse.success(isUpdated);
    }

    // 管理员条件查询用户列表
    @GetMapping("listUsers")
    public JsonResponse listUsers(PageDTO pageDTO, User user) {
        Page<User> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = userService.listUsers(page, user);
        return JsonResponse.success(page);
    }

    // 冻结账号
    @GetMapping("freezeById")
    public JsonResponse freezeById(Long id) {
        boolean res = userService.freezeById(id);
        return JsonResponse.success(res);
    }

    // 解冻账号
    @GetMapping("unfreezeById")
    public JsonResponse unfreezeById(Long id) {
        boolean res = userService.unfreezeById(id);
        return JsonResponse.success(res);
    }

    // 删除账号
    @GetMapping("deleteById")
    public JsonResponse deleteById(Long id) {
        boolean res = userService.removeById(id);
        return JsonResponse.success(res);
    }

    //根据id查询他人用户信息
    @GetMapping("/getOtherUserInfo")
    public JsonResponse getOtherUserInfo(Long id) {
        User user = userService.getOtherUserInfo(id);
        return JsonResponse.success(user);
    }

    //统计每年用户注册数
    @GetMapping("annualRegistrations")
    public JsonResponse getAnnualRegistrations(Integer startYear, Integer endYear) {
        List<YesrCountDTO> list = userService.getAnnualRegistrations(startYear, endYear);
        return JsonResponse.success(list);
    }

    //统计每年用户活跃数
    @GetMapping("/userActivityByYear")
    public JsonResponse<List<YearUserActivityDTO>> userActivityByYear(Integer startYear, Integer endYear) {
        List<YearUserActivityDTO> list = userService.getUserActivityByYear(startYear, endYear);
        return JsonResponse.success(list);
    }

    //获取用户收藏的商家列表
    @GetMapping("getPrivacySettingsByUserId")
    public JsonResponse getPrivacySettingsById(Long userId) {
        Integer privacySetting = userService.getPrivacySettingsById(userId);
        return JsonResponse.success(privacySetting);
    }
}